from django import forms

from .models import Category, Post, Tag


class TagForm(forms.Form):
    tags = forms.CharField(
        label='标签(以空格分隔)',
        widget=forms.TextInput(attrs={'class': 'form-control'})
    )


class PostForm(forms.ModelForm):
    class Meta:
        model = Post
        fields = ('title', 'img', 'content')
        label = {
            'title': '标题',
            'title_image': '标题图',
            'content': '正文',
        }
        widgets = {
            'title': forms.TextInput(attrs={'class': 'form-control'}),
            'content': forms.Textarea(attrs={'class': 'form-control'}),
        }

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # https://docs.djangoproject.com/en/3.0/topics/forms/modelforms/
        # ModelChoiceField一对多字段
        self.fields['category'] = forms.ModelChoiceField(
            label='栏目',
            required=False,
            queryset=Category.objects.all(),
            widget=forms.Select(attrs={'class': 'form-control custom-select'})
        )
        # ModelMultipleChoiceField多对多字段
        self.fields['tags'] = forms.ModelMultipleChoiceField(
            label='标签',
            required=False,
            queryset=Tag.objects.all(),
            widget=forms.SelectMultiple(attrs={'class': 'form-control'})
        )


class CategoryForm(forms.ModelForm):
    description = forms.CharField(
        label='简短描述（可不填）',
        required=False,
        widget=forms.Textarea(attrs={'rows': 4, 'class': 'form-control'})
    )

    class Meta:
        model = Category
        fields = ('name', 'description', 'is_nav')
        widgets = {
            'name': forms.TextInput(attrs={'class': 'form-control'}),
        }
